home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_6.zip / LOAD.C < prev    next >
C/C++ Source or Header  |  1993-07-02  |  4KB  |  146 lines

  1. /*
  2.     GWAda Development Environment for 386/486 PCs   
  3.     Copyright (C) 1993, Arthur Vargas Lopes  & Michael Bliss Feldman
  4.                         vlopes@vortex.ufrgs.br mfeldman@seas.gwu.edu
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; version 2 of the License.    
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  15.     You should have received a copy of the GNU General Public License
  16.     along with this program; if not, write to the Free Software
  17.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* load.c */
  21.  
  22. #include <\gwada\env\externs.h>
  23.  
  24.  
  25.  
  26.  
  27. void AVL_PROCESS_LOAD() 
  28. {
  29.     AVL_EDIT_WINDOW_PTR w;
  30.     AVL_WIN_PTR m1;
  31.     int n1, n2;
  32.     static char *msg = " GWAda - Enter file name to load, please: ";
  33.     char f[61];
  34.     w = &avl_windows[avl_window];
  35.     n1 = strlen(msg) + 10;
  36.     if (n1 < 62) n1 = 62;
  37.     n2 = (80 - n1) / 2;
  38.     m1 = AVL_MAKE_WINDOW(msg,7,n2,9,n1+n2,avl_wnd_bk_color,avl_wnd_color);
  39.     _settextposition(1,1);
  40.     f[0] = '\0';
  41.     AVL_PROMPT(1,1,f,60);
  42.     AVL_DEL_WINDOW(m1);
  43.     if (strlen(f) > 0) {
  44.         AVL_FREE_ALL();
  45.         AVL_LOAD_FILE(f);
  46.         }    
  47. }
  48.  
  49.  
  50.  
  51. void AVL_LOAD_FILE(char *fn)
  52. {
  53.         AVL_LINE_PTR head = NULL, temp;
  54.         int x ;
  55.         AVL_EDIT_WINDOW_PTR w;
  56.         w = &avl_windows[avl_window];
  57.         temp = AVL_MAKE_LINE("### DUMMY ###",0);
  58.         AVL_LINE_INSERT(temp,&head);
  59.         x = AVL_LOAD(fn, &head);
  60.         if (head == head -> next)  {
  61.             temp = AVL_MAKE_LINE("",2);
  62.             AVL_LINK(temp,head);
  63.             }
  64.         strcpy(avl_windows[avl_window].file_name,fn);
  65.         AVL_INIT_WINDOW(&avl_windows[avl_window], head -> next);
  66.         AVL_CURSOR_HOME();
  67.         AVL_UPDATE_SCREEN();
  68. }
  69.  
  70.  
  71. int AVL_LOAD(char *filen, AVL_LINE_PTR *at)
  72. {
  73.     AVL_WIN_PTR win;
  74.     AVL_LINE_PTR temp, temp2 = NULL;
  75.     int fp, i;
  76.     int line_no = 0;
  77.     long fsize;
  78.     char *buf = NULL;
  79.     char garbage = ' ';
  80.     char line[1024];
  81.     unsigned int size, n, j;
  82.     fp = open(filen,O_BINARY | O_RDWR, S_IREAD | S_IWRITE);
  83.     if (fp == -1) return 0;
  84.     sprintf(line,"Reading %s ...", filen);
  85.     win = AVL_MAKE_WINDOW("",23,1,25,strlen(line)+3,avl_wnd_bk_color,avl_wnd_color);
  86.     _setbkcolor(avl_msg_bk_color);
  87.     _settextcolor(avl_msg_color);
  88.     _settextposition(1,1);
  89.     _outtext(line);
  90.     lseek(fp,0L,2);
  91.     fsize = tell(fp);
  92.     if (fsize > 141000L)  {
  93.         AVL_ERROR("Source file too long (> 141,000 characters)!");
  94.         AVL_DEL_WINDOW(win);
  95.         close(fp);
  96.         return 0;
  97.         }
  98.     avl_windows[avl_window].buffer_size = fsize;
  99.     lseek(fp,0L,0);
  100.     size = fsize;
  101.     if (size == 0)
  102.         size = 10;
  103.     if ((buf = malloc(size)) == NULL)    {
  104.         AVL_ERROR("Out of memory to store file.");
  105.         AVL_DEL_WINDOW(win);
  106.         close(fp);
  107.         return -1;
  108.         }
  109.     if (fsize == 0L)
  110.         strcpy(buf,"   ");
  111.     while (fsize > 0L)  {
  112.         n = read(fp,buf,size);
  113.         if (n != size) {
  114.             AVL_ERROR("Can't read file.");
  115.             AVL_DEL_WINDOW(win);
  116.             close(fp);
  117.             return(0);
  118.             }
  119.         j = 0;
  120.         while (j < n)  {
  121.             for(i = 0; i < avl_start_pos; ++i)
  122.                 line[i] = ' ';
  123.             for (; !((buf[j] == 13) || (buf[j] == 10) || (j >= n)); ++i, ++j)
  124.                 line[i] = buf[j];
  125.             if ((buf[j] == 13) && (buf[j + 1] == 10)) ++j;
  126.             line[i] = '\0';
  127.             ++j;
  128.             temp = AVL_MAKE_LINE(line,line_no);
  129.             AVL_LINE_INSERT(temp, (temp2 == NULL) ? at : &temp2);
  130.             temp2 = temp;
  131.             }
  132.         fsize -= n;
  133.         }
  134.     if (buf[n-1] == '\n' || buf[n-1] == 10)  {
  135.         temp = AVL_MAKE_LINE("",line_no);
  136.         AVL_LINE_INSERT(temp, (temp2 == NULL) ? at : &temp2);
  137.         }
  138.     close(fp);
  139.     if (buf != NULL)
  140.         free(buf);
  141.     AVL_DEL_WINDOW(win);
  142.     return 1;
  143. }
  144.  
  145.  
  146.